package com.house.controller;

import com.house.common.R;
import com.house.entity.Menu;
import com.house.service.MenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    /**
     * 保存菜单
     * @param menu
     * @return
     */
    @PostMapping("/save")
    public R<String> save(@RequestBody Menu menu) {
        menuService.save(menu);
        return R.success("添加成功!");
    }

    /**
     * 更新菜单
     * @param menu
     * @return
     */
    @PutMapping("/update")
    public R<String> update(@RequestBody Menu menu) {
        menuService.update(menu);
        return R.success("更新成功");
    }

    @GetMapping("/getMenuByMenuId/{menuId}")
    public R<Menu> getMenuByMenuId(@PathVariable int menuId) {
        Menu menu = menuService.getMenuByMenuId(menuId);
        return R.success(menu);
    }

    /**
     * 获取所有菜单
     * @return
     */
    @GetMapping("/getMenus")
    public R<List> getMenus() {
        List menus = menuService.getMenus();
        return R.success(menus);
    }

    /**
     * 获取用户授权菜单
     * @return
     */
    @GetMapping("/getUserMenus")
    public R<List> getUserMenus() {
        List menus = menuService.getMenusByRole();
        if (menus == null) {
            return R.error("角色未添加菜单");
        }
        return R.success(menus);
    }

    /**
     * 删除菜单
     * @param menuId
     * @return
     */
    @DeleteMapping("/delete/{menuId}")
    public R<String> delete(@PathVariable int menuId) {
        int res = menuService.delete(menuId);
        if (res == 0) {
            return R.error("删除失败!");
        }
        return R.success("删除成功!");
    }
}
